﻿KISSY.use("validation, calendar, fgt/auth, fgt/ui", function (S, Validation, Calendar, Auth, UI) {
    S.ready(function (S) {
        var $ = S.all,
            J = jQuery,
            form = $("#J_BookFrom"),
            VALID_EFFECT = "Simple",
            VALID_STYLE = "s_null",
            validator = new Validation(form[0], { warn: VALID_EFFECT, style: VALID_STYLE }),
            submitBtn = $("#J_BookIt"),
            popbox = UI.getPopbox();


        validator.add("#checkin",
        {
            required: [true, "Checkin is required."],
            date: ["Invalid checkin."]
        });

        validator.add("#checkout",
        {
            required: [true, "Checkout is required."],
            date: ["Invalid checkout."]
        });

        function submit() {
            Auth.validate(function () {
                try {
                    var paramStr = S.ajax.serialize(form),
                        resultCode = FGT.core.constant.jsonResultCode,
                        failureText = "Book failure, please try again later.",
                        delay = 1000;

                    J.ajax({
                        url: "/rv/book",
                        type: "POST",
                        data: paramStr,
                        beforeSend: function (jqXHR, settings) {
                            submitBtn.prop("disabled", true);
                            popbox.show("Booking");
                        },
                        success: function (data, textStatus, jqXHR) {
                            if (data.Result === resultCode.SUCCESS) {
                                popbox.hide();
                            }
                            else {
                                var jsonData = data.Content;
                                S.each(jsonData, function (item) {
                                    var msgBox = $("#" + item.id);
                                    if (msgBox) {
                                        msgBox.text(item.msg);
                                        msgBox.removeClass("ok tip error");
                                        msgBox.addClass("error");
                                        msgBox.show();
                                    }
                                });
                                popbox.hide();
                                submitBtn.prop("disabled", false);
                            }
                        },
                        error: function (jqXHR, textStatus, errorThrown) {
                            popbox.show(failureText, delay);
                            submitBtn.prop("disabled", false);
                        }
                    });
                }
                catch (e) {
                    popbox.show(failureText, delay);
                    submitBtn.prop("disabled", false);
                }
            }, popbox, this);
        }

        submitBtn.on("click", function (event) {
            event.halt();
            var self = this,
                b = validator.isValid();
            if (b === true) {
                submit();
            }
        });


        var currentDate = new Date(),
            year = currentDate.getFullYear(),
            month = currentDate.getMonth(),
            day = currentDate.getDate(),
            minDate = currentDate,
            maxDate = new Date(year + 10, month, day),

            checkinInput = $("#checkin"),
            checkinCalendar,
            defaultCheckin = currentDate,

            checkoutInput = $("#checkout"),
            checkoutCalendar,
            defaultCheckout = currentDate;

        checkinCalendar = new Calendar(checkinInput, {
            date: defaultCheckin,
            selected: defaultCheckin,
            minDate: minDate,
            maxDate: maxDate,
            popup: true,
            closable: true,
            align: { points: ['bl', 'tl'], offset: [0, 0] }
        });

        checkoutCalendar = new Calendar(checkoutInput, {
            date: defaultCheckout,
            selected: defaultCheckout,
            minDate: minDate,
            maxDate: maxDate,
            popup: true,
            closable: true,
            align: { points: ['bl', 'tl'], offset: [0, 0] }
        });

        checkinCalendar.on("select", function (event) {
            checkinInput.val(S.Date.format(event.date, "isoDate"));
        });

        checkoutCalendar.on("select", function (event) {
            checkoutInput.val(S.Date.format(event.date, "isoDate"));
        });
    });
});